{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "-201"
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from iFinDPy import *\n",
    "today=datetime.date.today().strftime(\"%Y-%M-%d\")\n",
    "THS_iFinDLogin('bgf003','122180')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0    000001.SZ\n1    000002.SZ\n2    000004.SZ\n3    000005.SZ\n4    000006.SZ\nName: THSCODE, dtype: object"
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "all_stock_list=THS_Trans2DataFrame(THS_DataPool('block','2020-05-22;001005010','thscode:Y,security_name:Y',True))['THSCODE']\n",
    "all_stock_list.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "factor_list=['ths_inflow_amt_stock','ths_outflow_amt_stock','ths_inflow_vol_stock','ths_outflow_vol_stock','ths_net_buy_amt_stock','ths_net_buy_vol_stock','ths_active_buy_amt_stock','ths_active_sell_amt_stock','ths_active_buy_vol_stock','ths_active_sell_vol_stock','ths_net_active_buy_amt_stock','ths_net_active_buy_vol_stock','ths_active_buy_large_vol_stock','ths_active_buy_large_amt_stock','ths_possitive_buy_large_vol_stock','ths_possitive_buy_large_amt_stock','ths_active_sell_large_vol_stock','ths_active_sell_large_amt_stock','ths_possitive_sell_large_vol_stock','ths_possitive_sell_large_amt_stock','ths_active_buy_main_vol_stock','ths_active_buy_main_amt_stock','ths_possitive_buy_main_vol_stock','ths_possitive_buy_main_amt_stock','ths_active_sell_main_vol_stock','ths_active_sell_main_amt_stock','ths_possitive_sell_main_vol_stock','ths_possitive_sell_main_amt_stock','ths_active_buy_middle_vol_stock','ths_active_buy_middle_amt_stock','ths_possitive_buy_middle_vol_stock','ths_possitive_buy_middle_amt_stock','ths_active_sell_middle_vol_stock','ths_active_sell_middle_amt_stock','ths_possitive_sell_middle_vol_stock','ths_possitive_sell_middle_amt_stock','ths_active_buy_small_vol_stock','ths_active_buy_small_amt_stock','ths_active_sellsmall_vol_stock','ths_active_sellsmall_amt_stock','ths_small_buy_vol_stock','ths_small_buy_amt_stock','ths_small_sell_vol_stock','ths_small_sell_amt_stock','ths_dde_stock','ths_dde_net_vol_stock','ths_net_infow_stock','ths_dde_5d_stock','ths_dde_10d_stock','ths_dde_20d_stock','ths_net_active_buy_amt_rate_stock','ths_net_active_buy_amt_ratio_stock','ths_net_active_buy_vol_rate_stock','ths_net_active_buy_vol_ratio_stock','ths_inflow_rate_stock','ths_main_net_inflow_amt_ratio_stock','ths_main_inflow_rate_stock','ths_main_net_inflow_vol_ratio_stock']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for factor in factor_list[-10:]:\n",
    "#     df=THS_Trans2DataFrame(THS_DateSerial(all_stock_list.to_list(),factor,'','Days:Tradedays,Fill:Previous,Interval:D','2015-01-01','2020-05-22',True))\n",
    "#     print(df.head())\n",
    "#     df['thscode']=[i[:6]for i in df['thscode']]\n",
    "#     df.set_index(['time','thscode'])[factor].unstack()\n",
    "#     break\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "JSONDecodeError",
     "evalue": "Expecting value: line 1 column 1 (char 0)",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mJSONDecodeError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-92-0a1d8eae397f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mstock\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mall_stock_list\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[0mdf\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mTHS_Trans2DataFrame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mTHS_DateSerial\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstock\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfactor_str\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'100'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'Days:Tradedays,Fill:Previous,Interval:D'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'2020-01-01'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'2020-05-22'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m     \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Desktop\\DataInterface_free_Windows_20200420\\DataInterface_free_Windows\\bin\\x64\\iFinDPy.py\u001b[0m in \u001b[0;36mTHS_Trans2DataFrame\u001b[1;34m(data)\u001b[0m\n\u001b[0;32m    189\u001b[0m             \u001b[0mout\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miFinD\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdecodeMethod\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'ignore'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    190\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mout\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 191\u001b[1;33m         \u001b[0mout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mout\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mobject_pairs_hook\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mOrderedDict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    192\u001b[0m         \u001b[0mdataframe\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    193\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mdataframe\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\json\\__init__.py\u001b[0m in \u001b[0;36mloads\u001b[1;34m(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[0;32m    359\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mparse_constant\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    360\u001b[0m         \u001b[0mkw\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'parse_constant'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mparse_constant\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 361\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\json\\decoder.py\u001b[0m in \u001b[0;36mdecode\u001b[1;34m(self, s, _w)\u001b[0m\n\u001b[0;32m    335\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    336\u001b[0m         \"\"\"\n\u001b[1;32m--> 337\u001b[1;33m         \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mraw_decode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0midx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0m_w\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    338\u001b[0m         \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_w\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    339\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mend\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\json\\decoder.py\u001b[0m in \u001b[0;36mraw_decode\u001b[1;34m(self, s, idx)\u001b[0m\n\u001b[0;32m    353\u001b[0m             \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscan_once\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0midx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    354\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mStopIteration\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 355\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mJSONDecodeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Expecting value\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    356\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)"
     ]
    }
   ],
   "source": [
    "for factor in factor_list:\n",
    "    data_sum_list=[]\n",
    "    for stock in all_stock_list:\n",
    "        df=THS_Trans2DataFrame(THS_DateSerial(stock,factor,'100','Days:Tradedays,Fill:Previous,Interval:D','2020-01-01','2020-05-22',True))\n",
    "        data_sum_list.append(df)\n",
    "    data_sum_df=pd.concat(data_sum_list)\n",
    "    print(data_sum_df.head())\n",
    "    break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "price_panel.to_pickle(\"price_panel.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<class 'pandas.core.panel.Panel'>\nDimensions: 9 (items) x 59 (major_axis) x 3959 (minor_axis)\nItems axis: open to limit_up\nMajor_axis axis: 2010-01-04 00:00:00 to 2010-04-01 00:00:00\nMinor_axis axis: 300275.XSHE to 600386.XSHG"
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "source": [
    "price_panel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'2020-00-10'"
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python37464bitbasecondaf58f680e650245d6af9156a7f17ac0fa",
   "display_name": "Python 3.7.4 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}